/*******************************************************************************
 * Copyright (c) 2006 CEA List.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     CEA List - initial API and implementation
 *******************************************************************************/
package com.cea.papyrus.diagram.usecase;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.gef.palette.MarqueeToolEntry;
import org.eclipse.gef.palette.PaletteContainer;
import org.eclipse.gef.palette.PaletteDrawer;
import org.eclipse.gef.palette.PaletteEntry;
import org.eclipse.gef.palette.PaletteGroup;
import org.eclipse.gef.palette.PaletteRoot;
import org.eclipse.gef.palette.PaletteSeparator;
import org.eclipse.gef.palette.PaletteStack;
import org.eclipse.gef.palette.PanningSelectionToolEntry;
import org.eclipse.gef.palette.ToolEntry;
import org.eclipse.gef.ui.palette.FlyoutPaletteComposite.FlyoutPreferences;
import org.eclipse.jface.preference.IPreferenceStore;
import org.eclipse.uml2.uml.Abstraction;
import org.eclipse.uml2.uml.Actor;
import org.eclipse.uml2.uml.Association;
import org.eclipse.uml2.uml.Comment;
import org.eclipse.uml2.uml.Constraint;
import org.eclipse.uml2.uml.Dependency;
import org.eclipse.uml2.uml.Extend;
import org.eclipse.uml2.uml.ExtensionPoint;
import org.eclipse.uml2.uml.Generalization;
import org.eclipse.uml2.uml.Include;
import org.eclipse.uml2.uml.Package;
import org.eclipse.uml2.uml.PackageImport;
import org.eclipse.uml2.uml.Realization;
import org.eclipse.uml2.uml.Usage;
import org.eclipse.uml2.uml.UseCase;

import com.cea.papyrus.core.IPapyrusUIConstants;
import com.cea.papyrus.core.PapyrusMessages;
import com.cea.papyrus.core.PapyrusPlugin;
import com.cea.papyrus.core.editor.CreationEntryFactory;
import com.cea.papyrus.core.editor.DiagramPalette;
import com.cea.papyrus.core.pseudoelement.GraphicalLink;
import com.cea.papyrus.core.utils.Debug;

/**
 * Manages creation of the palette in the use case diagram editor.
 */
public final class UseCaseDiagramPaletteFactory extends CreationEntryFactory {

	/** debug channel */
	private static final int channel = 29;

	/** Preference ID used to persist the palette location */
	private static final String PALETTE_DOCK_LOCATION = "ShapesEditorPaletteFactory.Location";

	/**Preference ID used to persist the palette size */
	private static final String PALETTE_SIZE = "ShapesEditorPaletteFactory.Size";

	/** Preference ID used to persist the flyout palette's state */
	private static final String PALETTE_STATE = "ShapesEditorPaletteFactory.State";

	/**
	 * Creates the PaletteRoot and adds all palette elements. Use this factory
	 * method to create a new palette for your graphical editor.
	 * @return a new PaletteRoot
	 */
	public static DiagramPalette createPalette() {
		Debug.debug(null, "Call UseCaseDiagramPaletteFactory.createPalette()", channel);
		DiagramPalette palette = new DiagramPalette(
				IPapyrusUIConstants.USE_CASE_DIAGRAM);
		palette.add(createToolsGroup(palette));
		palette.add(createShapesDrawer());

		// Create additionnal menu defined in extensions
		palette.createMenuExtensions();

		return palette;
	}

	/**
	 * Return a FlyoutPreferences instance used to save/load the preferences of
	 * a flyout palette.
	 * @return the flyout preferences for the palette in use case diagrams
	 */
	static FlyoutPreferences createPalettePreferences() {
		Debug.debug(null,"Call UseCaseDiagramPaletteFactory.createPalettePreferences()", channel);
		return new FlyoutPreferences() {
			
			/* (non-Javadoc)
			 * @see org.eclipse.gef.ui.palette.FlyoutPaletteComposite$FlyoutPreferences#getDockLocation()
			 */
			public int getDockLocation() {
				return getPreferenceStore().getInt(PALETTE_DOCK_LOCATION);
			}

			/* (non-Javadoc)
			 * @see org.eclipse.gef.ui.palette.FlyoutPaletteComposite$FlyoutPreferences#getPaletteState()
			 */
			public int getPaletteState() {
				return getPreferenceStore().getInt(PALETTE_STATE);
			}

			/* (non-Javadoc)
			 * @see org.eclipse.gef.ui.palette.FlyoutPaletteComposite$FlyoutPreferences#getPaletteWidth()
			 */
			public int getPaletteWidth() {
				return getPreferenceStore().getInt(PALETTE_SIZE);
			}

			/**
			 * Returns the preference store associated to the plugin
			 * @return the preference store associated to the plugin
			 */
			private IPreferenceStore getPreferenceStore() {
				return Activator.getDefault().getPreferenceStore();
			}

			/* (non-Javadoc)
			 * @see org.eclipse.gef.ui.palette.FlyoutPaletteComposite$FlyoutPreferences#setDockLocation(int)
			 */
			public void setDockLocation(int location) {
				getPreferenceStore().setValue(PALETTE_DOCK_LOCATION, location);
			}

			/* (non-Javadoc)
			 * @see org.eclipse.gef.ui.palette.FlyoutPaletteComposite$FlyoutPreferences#setPaletteState(int)
			 */
			public void setPaletteState(int state) {
				getPreferenceStore().setValue(PALETTE_STATE, state);
			}

			/* (non-Javadoc)
			 * @see org.eclipse.gef.ui.palette.FlyoutPaletteComposite$FlyoutPreferences#setPaletteWidth(int)
			 */
			public void setPaletteWidth(int width) {
				getPreferenceStore().setValue(PALETTE_SIZE, width);
			}
		};
	}

	/**
	 * Creates the palette container containing uml node elements creations tools
	 * @return the palette container containing uml node elements creations tools
	 */
	private static PaletteContainer createShapesDrawer() {
		Debug.debug(null,"Call UseCaseDiagramPaletteFactory.createShapesDrawer()", channel);
		PaletteDrawer componentsDrawer = new PaletteDrawer("Uml Element");
		
		List<PaletteEntry> entries = new ArrayList<PaletteEntry>();
		try {
			// Package creation
			entries.add(PapyrusPlugin.creationToolFactory.getCombinedTemplateCreationToolEntry(
							PapyrusMessages.AddPackageCommand_Label,
							PapyrusMessages.AddPackageCommand_Description,
							Package.class, 
							null));
			
			// Actor creation
			entries.add(PapyrusPlugin.creationToolFactory.getCombinedTemplateCreationToolEntry(
					PapyrusMessages.AddActorCommand_Label,
					PapyrusMessages.AddActorCommand_Description,
							Actor.class, 
							null));
			
			// UseCase creation
			entries.add(PapyrusPlugin.creationToolFactory.getCombinedTemplateCreationToolEntry(
							PapyrusMessages.AddUseCaseCommand_Label,
							PapyrusMessages.AddUseCaseCommand_Description,
							UseCase.class, 
							null));
			
			// ExtensionPoint creation
			entries.add(PapyrusPlugin.creationToolFactory.getCombinedTemplateCreationToolEntry(
					PapyrusMessages.AddExtensionPointCommand_Label,
					PapyrusMessages.AddExtensionPointCommand_Description,
							ExtensionPoint.class, 
							null));
			
			// TODO Component creation, classifier form	with a palette stack
			// Class creation, classifier form
			entries.add(PapyrusPlugin.creationToolFactory.getCombinedTemplateCreationToolEntry(
					PapyrusMessages.AddClassCommand_Label,
					PapyrusMessages.AddClassCommand_Description,
							org.eclipse.uml2.uml.Class.class, 
							null));			
		
			
			// Comment creation
			entries.add(PapyrusPlugin.creationToolFactory.getCombinedTemplateCreationToolEntry(
					PapyrusMessages.AddCommentCommand_Label,
					PapyrusMessages.AddCommentCommand_Description,
							Comment.class, 
							null));
			
			// Constraint creation
			entries.add(PapyrusPlugin.creationToolFactory.getCombinedTemplateCreationToolEntry(
					PapyrusMessages.AddConstraintCommand_Label,
					PapyrusMessages.AddConstraintCommand_Description,
							Constraint.class, 
							null));		 
		} catch (Exception e) {
			Debug.log(e);
		}
		componentsDrawer.addAll(entries);
		return componentsDrawer;
	}

	/**
	 * Create the "Tools" group.
	 * @param the palette where to add the elements
	 * @return the palette container with the tools group 
	 */
	private static PaletteContainer createToolsGroup(PaletteRoot palette) {
		Debug.debug(null,"Call UseCaseDiagramPaletteFactory.createToolsGroup()", channel);

		PaletteGroup toolGroup = new PaletteGroup("RelationShips");

		// Add a selection tool to the group
		ToolEntry tool = new PanningSelectionToolEntry();
		toolGroup.add(tool);
		palette.setDefaultEntry(tool);

		// Add a marquee tool to the group
		toolGroup.add(new MarqueeToolEntry());

		// Add a (unnamed) separator to the group
		toolGroup.add(new PaletteSeparator());

		List<PaletteEntry> entries = new ArrayList<PaletteEntry>();
		try {
			///////////////////////////////////////////////////////////////////
			// Begin of entry stack for Dependencies
			///////////////////////////////////////////////////////////////////
			PaletteStack paletteStack = new PaletteStack("", "", null);
			
			// Add Dependency connection tool
			paletteStack.add(PapyrusPlugin.creationToolFactory.getConnectionCreationToolEntry(
					PapyrusMessages.AddDependencyCommand_Label,
					PapyrusMessages.AddDependencyCommand_Description,
					Dependency.class,
					null));

			// Add Abstraction connection tool
			paletteStack.add(PapyrusPlugin.creationToolFactory.getConnectionCreationToolEntry(
					PapyrusMessages.AddAbstractionCommand_Label,
					PapyrusMessages.AddAbstractionCommand_Description,
					Abstraction.class,
					null));
			

			// Add Realization connection tool
			paletteStack.add(PapyrusPlugin.creationToolFactory.getConnectionCreationToolEntry(
					PapyrusMessages.AddRealizationCommand_Label,
					PapyrusMessages.AddRealizationCommand_Description,
					Realization.class,
					null));
			
			
			// Add Usage connection tool
			paletteStack.add(PapyrusPlugin.creationToolFactory.getConnectionCreationToolEntry(
					PapyrusMessages.AddUsageCommand_Label,
					PapyrusMessages.AddUsageCommand_Description,
					Usage.class,
					null));	
			
			// Add PackageImport
			paletteStack
					.add(PapyrusPlugin.creationToolFactory
							.getConnectionCreationToolEntry(
									PapyrusMessages.AddPackageImportCommand_Label,
									PapyrusMessages.AddPackageImportCommand_Description,
									PackageImport.class, 
									null));
			
			entries.add(paletteStack);	
			
			///////////////////////////////////////////////////////////////////
			// End of entry stack for Dependencies
			///////////////////////////////////////////////////////////////////

			// generalization Stack
			entries.add(PapyrusPlugin.creationToolFactory.getConnectionCreationToolEntry(
					PapyrusMessages.AddGeneralizationCommand_Label
					, PapyrusMessages.AddGeneralizationCommand_Description
					, Generalization.class
					, null));
			
			// Extend palette stack
			paletteStack = new PaletteStack("", "", null);
			// Add Extend
			paletteStack.add(PapyrusPlugin.creationToolFactory.getConnectionCreationToolEntry(
					PapyrusMessages.AddExtendCommand_Label,
					PapyrusMessages.AddExtendCommand_Description,
							Extend.class, 
							null));
			// Add Include
			paletteStack.add(PapyrusPlugin.creationToolFactory.getConnectionCreationToolEntry(
					PapyrusMessages.AddIncludeCommand_Label,
					PapyrusMessages.AddIncludeCommand_Description,
							Include.class, 
							null));
			
			entries.add(paletteStack);
			// Extend palette stack - End
			
			// Add Association
			entries.add(PapyrusPlugin.creationToolFactory.getConnectionCreationToolEntry(
					PapyrusMessages.AddAssociationCommand_Label,
					PapyrusMessages.AddAssociationCommand_Description,
							Association.class, 
							null));


			///////////////////////////////////////////////////////////////////
			// Begin of tool for Comment and constraint links
			///////////////////////////////////////////////////////////////////
			
			// Add GraphicalLink
			entries.add(PapyrusPlugin.creationToolFactory.getConnectionCreationToolEntry(
					PapyrusMessages.AddGraphicalLinkCommand_Label,
					PapyrusMessages.AddGraphicalLinkCommand_Description,
					GraphicalLink.class,
					null));	
			
		} catch (Exception e) {
			Debug.log(e);
		}

		toolGroup.addAll(entries);
		return toolGroup;
	}
}